﻿using BLL;
using Inspinia_MVC5.Code;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Inspinia_MVC5.Controllers
{
    [AllowCors]
    [SessionTimeOutFilter]
    public class WellListController : Controller
    {
        //个人所有井数据
        public JsonResult Well()
        {
            WellChinaService WellChinaService = new WellChinaService();
            var well = WellChinaService.All();
            if (Session["identity"].ToString() == "Admin")
            {
                return Json(new { status = "0", data = well }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"] != null)
            {
                string wellstr = Session["wellstr"].ToString();
                well = well.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-") && c.BLOCK_ID != -1);
                return Json(new { status = "0", data = well }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult WellOfBlock(int blockid)
        {
            WellChinaService WellChinaService = new WellChinaService();
            TemperatureSensorService TemperatureSensor = new TemperatureSensorService();
            BlockService BlockService = new BlockService();
            PipeService Pipe = new PipeService();
            TankService Tank = new TankService();
            var well = WellChinaService.All();
            var block = BlockService.Find(c => c.ID == blockid);
            var tsensor = TemperatureSensor.All();
            tsensor = tsensor.Where(c => c.BLOCK_ID != -1 && c.BLOCK_ID == blockid);
            var pipe = Pipe.All();
            pipe = pipe.Where(c => c.BLOCK_ID != -1 && c.BLOCK_ID == blockid);
            var tank = Tank.All();
            tank = tank.Where(c => c.BLOCK_ID != -1 && c.BLOCK_ID == blockid);

            if (Session["identity"].ToString() == "Admin")
            {
                well = well.Where(c => c.BLOCK_ID != -1 && c.BLOCK_ID == blockid).OrderBy(c => c.ID);

                return Json(new { status = "0", oneblock = block, data = well, data1 = tsensor, data2 = pipe, data3 = tank }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"] != null)
            {
                string wellstr = Session["wellstr"].ToString();
                well = well.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-") && c.BLOCK_ID != -1 && c.BLOCK_ID == blockid).OrderBy(c => c.ID);

                return Json(new { status = "0", oneblock = block, data = well, data1 = tsensor, data2 = pipe, data3 = tank }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
        }

        public JsonResult SearchWell(string str)
        {
            WellChinaService WellChinaService = new WellChinaService();
            var well = WellChinaService.All();
            if (Session["identity"].ToString() == "Admin")
            {
                well = well.Where(c => c.Name1.Contains(str)||c.Name2.Contains(str)).OrderBy(c => c.ID);
                return Json(new { status = "0",  data = well}, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"] != null)
            {
                string wellstr = Session["wellstr"].ToString();
                well = well.Where(c => wellstr.Contains("-" + c.ID.ToString() + "-") && (c.Name1.Contains(str) || c.Name2.Contains(str))).OrderBy(c => c.ID);
                return Json(new { status = "0",data = well}, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult Tsensor(int sensorid)
        {
            TemperatureSensorService TemperatureSensor = new TemperatureSensorService();
            var tsensor = TemperatureSensor.All();
            tsensor = tsensor.Where(c => c.Id == sensorid);
            return Json(new { status = "0", data = tsensor }, JsonRequestBehavior.AllowGet);
        }
        public JsonResult OneWellOfBlock(int wellid)
        {
            WellChinaService WellChinaService = new WellChinaService();
            BlockService BlockService = new BlockService();
            var well = WellChinaService.Find(c => c.ID == wellid);
            var block = BlockService.Find(c => c.ID == well.BLOCK_ID);
            string wellstr = Session["wellstr"].ToString();
            if (Session["identity"].ToString() == "Admin")
            {
                return Json(new { status = "0", oneblock = block, data = well }, JsonRequestBehavior.AllowGet);
            }
            else if (Session["identity"] != null && wellstr.Contains("-" + well.ID.ToString() + "-"))
            {
                return Json(new { status = "0", oneblock = block, data = well }, JsonRequestBehavior.AllowGet);
            }
            else
            {
                return Json(new { status = "7" }, JsonRequestBehavior.AllowGet);
            }
        }
        public JsonResult OneWell(int wellid)
        {
            WellChinaService WellChinaService = new WellChinaService();
            var well = WellChinaService.Find(c => c.ID == wellid);
            return Json(new { status = "0", data = well }, JsonRequestBehavior.AllowGet);
        }
        // GET: WellList
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Search()
        {
            return View();
        }
    }
}